<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="csv-fmt-params.html" />
<link rel="prev" href="module-csv.html" />
<link rel="parent" href="module-csv.html" />
<link rel="next" href="csv-fmt-params.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>9.1.1 Module Contents </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="9.1 csv  "
  href="module-csv.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="9.1 csv  "
  href="module-csv.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="9.1.2 dialects and Formatting"
  href="csv-fmt-params.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-csv.html">9.1 csv  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-csv.html">9.1 csv  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="csv-fmt-params.html">9.1.2 Dialects and Formatting</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0011110000000000000000"></a><a name="csv-contents"></a>
<br>
9.1.1 Module Contents 
</h2>

<p>
The <tt class="module">csv</tt> module defines the following functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2008' xml:id='l2h-2008' class="function">reader</tt></b>(</nobr></td>
  <td><var>csvfile</var><big>[</big><var>,
                         dialect=<code>'excel'</code></var><big>]</big><var></var><big>[</big><var>, fmtparam</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a reader object which will iterate over lines in the given
<var>csvfile</var>.  <var>csvfile</var> can be any object which supports the
iterator protocol and returns a string each time its <tt class="method">next</tt>
method is called -- file objects and list objects are both suitable.  
If <var>csvfile</var> is a file object, it must be opened with
the 'b' flag on platforms where that makes a difference.  An optional
<var>dialect</var> parameter can be given
which is used to define a set of parameters specific to a particular CSV
dialect.  It may be an instance of a subclass of the <tt class="class">Dialect</tt>
class or one of the strings returned by the <tt class="function">list_dialects</tt>
function.  The other optional <var>fmtparam</var> keyword arguments can be
given to override individual formatting parameters in the current
dialect.  For full details about the dialect and formatting
parameters, see section&nbsp;<a href="csv-fmt-params.html#csv-fmt-params">9.1.2</a>, ``Dialects and Formatting
Parameters''.

<p>
All data read are returned as strings.  No automatic data type
conversion is performed.

<p>

<span class="versionnote">Changed in version 2.5:

The parser is now stricter with respect to multi-line quoted
fields. Previously, if a line ended within a quoted field without a
terminating newline character, a newline would be inserted into the
returned field. This behavior caused problems when reading files
which contained carriage return characters within fields.  The
behavior was changed to return the field without inserting newlines. As
a consequence, if newlines embedded within fields are important, the
input should be split into lines in a manner which preserves the newline
characters.</span>

<p>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2009' xml:id='l2h-2009' class="function">writer</tt></b>(</nobr></td>
  <td><var>csvfile</var><big>[</big><var>,
                         dialect=<code>'excel'</code></var><big>]</big><var></var><big>[</big><var>, fmtparam</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a writer object responsible for converting the user's data into
delimited strings on the given file-like object.  <var>csvfile</var> can be any
object with a <tt class="function">write</tt> method.  If <var>csvfile</var> is a file object,
it must be opened with the 'b' flag on platforms where that makes a
difference.  An optional
<var>dialect</var> parameter can be given which is used to define a set of
parameters specific to a particular CSV dialect.  It may be an instance
of a subclass of the <tt class="class">Dialect</tt> class or one of the strings
returned by the <tt class="function">list_dialects</tt> function.  The other optional
<var>fmtparam</var> keyword arguments can be given to override individual
formatting parameters in the current dialect.  For full details
about the dialect and formatting parameters, see
section&nbsp;<a href="csv-fmt-params.html#csv-fmt-params">9.1.2</a>, ``Dialects and Formatting Parameters''.
To make it as easy as possible to
interface with modules which implement the DB API, the value
<tt class="constant">None</tt> is written as the empty string.  While this isn't a
reversible transformation, it makes it easier to dump SQL NULL data values
to CSV files without preprocessing the data returned from a
<code>cursor.fetch*()</code> call.  All other non-string data are stringified
with <tt class="function">str()</tt> before being written.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2010' xml:id='l2h-2010' class="function">register_dialect</tt></b>(</nobr></td>
  <td><var>name</var><big>[</big><var>, dialect</var><big>]</big><var></var><big>[</big><var>, fmtparam</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Associate <var>dialect</var> with <var>name</var>.  <var>name</var> must be a string
or Unicode object. The dialect can be specified either by passing a
sub-class of <tt class="class">Dialect</tt>, or by <var>fmtparam</var> keyword arguments,
or both, with keyword arguments overriding parameters of the dialect.
For full details about the dialect and formatting parameters, see
section&nbsp;<a href="csv-fmt-params.html#csv-fmt-params">9.1.2</a>, ``Dialects and Formatting Parameters''.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2011' xml:id='l2h-2011' class="function">unregister_dialect</tt></b>(</nobr></td>
  <td><var>name</var>)</td></tr></table></dt>
<dd>
Delete the dialect associated with <var>name</var> from the dialect registry.  An
<tt class="exception">Error</tt> is raised if <var>name</var> is not a registered dialect
name.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2012' xml:id='l2h-2012' class="function">get_dialect</tt></b>(</nobr></td>
  <td><var>name</var>)</td></tr></table></dt>
<dd>
Return the dialect associated with <var>name</var>.  An <tt class="exception">Error</tt> is
raised if <var>name</var> is not a registered dialect name.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2013' xml:id='l2h-2013' class="function">list_dialects</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Return the names of all registered dialects.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2014' xml:id='l2h-2014' class="function">field_size_limit</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>new_limit</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Returns the current maximum field size allowed by the parser. If
  <var>new_limit</var> is given, this becomes the new limit.
  
<span class="versionnote">New in version 2.5.</span>

</dl>

<p>
The <tt class="module">csv</tt> module defines the following classes:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2015' xml:id='l2h-2015' class="class">DictReader</tt></b>(</nobr></td>
  <td><var>csvfile</var><big>[</big><var>,
			      fieldnames=<tt class="constant">None</tt>,</var><big>[</big><var>,
                              restkey=<tt class="constant">None</tt></var><big>[</big><var>,
			      restval=<tt class="constant">None</tt></var><big>[</big><var>,
                              dialect=<code>'excel'</code></var><big>[</big><var>,
			      *args, **kwds</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create an object which operates like a regular reader but maps the
information read into a dict whose keys are given by the optional
 <var>fieldnames</var>
parameter.  If the <var>fieldnames</var> parameter is omitted, the values in
the first row of the <var>csvfile</var> will be used as the fieldnames.
If the row read has fewer fields than the fieldnames sequence,
the value of <var>restval</var> will be used as the default value.  If the row
read has more fields than the fieldnames sequence, the remaining data is
added as a sequence keyed by the value of <var>restkey</var>.  If the row read
has fewer fields than the fieldnames sequence, the remaining keys take the
value of the optional <var>restval</var> parameter.  Any other optional or
keyword arguments are passed to the underlying <tt class="class">reader</tt> instance.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2016' xml:id='l2h-2016' class="class">DictWriter</tt></b>(</nobr></td>
  <td><var>csvfile, fieldnames</var><big>[</big><var>,
                              restval=""</var><big>[</big><var>,
                              extrasaction=<code>'raise'</code></var><big>[</big><var>,
                              dialect=<code>'excel'</code></var><big>[</big><var>,
			      *args, **kwds</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create an object which operates like a regular writer but maps dictionaries
onto output rows.  The <var>fieldnames</var> parameter identifies the order in
which values in the dictionary passed to the <tt class="method">writerow()</tt> method are
written to the <var>csvfile</var>.  The optional <var>restval</var> parameter
specifies the value to be written if the dictionary is missing a key in
<var>fieldnames</var>.  If the dictionary passed to the <tt class="method">writerow()</tt>
method contains a key not found in <var>fieldnames</var>, the optional
<var>extrasaction</var> parameter indicates what action to take.  If it is set
to <code>'raise'</code> a <tt class="exception">ValueError</tt> is raised.  If it is set to
<code>'ignore'</code>, extra values in the dictionary are ignored.  Any other
optional or keyword arguments are passed to the underlying <tt class="class">writer</tt>
instance.

<p>
Note that unlike the <tt class="class">DictReader</tt> class, the <var>fieldnames</var>
parameter of the <tt class="class">DictWriter</tt> is not optional.  Since Python's
<tt class="class">dict</tt> objects are not ordered, there is not enough information
available to deduce the order in which the row should be written to the
<var>csvfile</var>.

<p>
</dl>

<p>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2017' xml:id='l2h-2017' class="class">Dialect</tt></b>
<dd>
The <tt class="class">Dialect</tt> class is a container class relied on primarily for its
attributes, which are used to define the parameters for a specific
<tt class="class">reader</tt> or <tt class="class">writer</tt> instance.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2018' xml:id='l2h-2018' class="class">excel</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
The <tt class="class">excel</tt> class defines the usual properties of an Excel-generated
CSV file.  It is registered with the dialect name <code>'excel'</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2019' xml:id='l2h-2019' class="class">excel_tab</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
The <tt class="class">excel_tab</tt> class defines the usual properties of an
Excel-generated TAB-delimited file.  It is registered with the dialect name
<code>'excel-tab'</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2020' xml:id='l2h-2020' class="class">Sniffer</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
The <tt class="class">Sniffer</tt> class is used to deduce the format of a CSV file.
</dl>

<p>
The <tt class="class">Sniffer</tt> class provides two methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2021' xml:id='l2h-2021' class="method">sniff</tt></b>(</nobr></td>
  <td><var>sample</var><big>[</big><var>,delimiters=None</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Analyze the given <var>sample</var> and return a <tt class="class">Dialect</tt> subclass
reflecting the parameters found.  If the optional <var>delimiters</var> parameter
is given, it is interpreted as a string containing possible valid delimiter
characters.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2022' xml:id='l2h-2022' class="method">has_header</tt></b>(</nobr></td>
  <td><var>sample</var>)</td></tr></table></dt>
<dd>
Analyze the sample text (presumed to be in CSV format) and return
<tt class="constant">True</tt> if the first row appears to be a series of column
headers.
</dl>

<p>
The <tt class="module">csv</tt> module defines the following constants:

<p>
<dl><dt><b><tt id='l2h-2023' xml:id='l2h-2023'>QUOTE_ALL</tt></b></dt>
<dd>
Instructs <tt class="class">writer</tt> objects to quote all fields.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2024' xml:id='l2h-2024'>QUOTE_MINIMAL</tt></b></dt>
<dd>
Instructs <tt class="class">writer</tt> objects to only quote those fields which contain
special characters such as <var>delimiter</var>, <var>quotechar</var> or any of the
characters in <var>lineterminator</var>.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2025' xml:id='l2h-2025'>QUOTE_NONNUMERIC</tt></b></dt>
<dd>
Instructs <tt class="class">writer</tt> objects to quote all non-numeric
fields. 

<p>
Instructs the reader to convert all non-quoted fields to type <var>float</var>.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-2026' xml:id='l2h-2026'>QUOTE_NONE</tt></b></dt>
<dd>
Instructs <tt class="class">writer</tt> objects to never quote fields.  When the current
<var>delimiter</var> occurs in output data it is preceded by the current
<var>escapechar</var> character.  If <var>escapechar</var> is not set, the writer
will raise <tt class="exception">Error</tt> if any characters that require escaping
are encountered.

<p>
Instructs <tt class="class">reader</tt> to perform no special processing of quote characters.
</dd></dl>

<p>
The <tt class="module">csv</tt> module defines the following exception:

<p>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2027' xml:id='l2h-2027' class="exception">Error</tt></b></dt>
<dd>
Raised by any of the functions when an error is detected.
</dd></dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="9.1 csv  "
  href="module-csv.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="9.1 csv  "
  href="module-csv.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="9.1.2 dialects and Formatting"
  href="csv-fmt-params.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-csv.html">9.1 csv  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-csv.html">9.1 csv  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="csv-fmt-params.html">9.1.2 Dialects and Formatting</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
